package com.project.light.schedule;

import com.project.light.dto.*;
import com.project.light.service.DataService;
import com.project.light.service.GroupPlanService;
import com.project.light.service.GroupService;
import com.project.light.service.StationInfoService;
import com.project.light.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * - 定时任务
 */
@Slf4j
@Component
@EnableScheduling
public class Schedule {
    @Resource
    StationInfoService stationInfoService;
    @Resource
    GroupService groupService;
    @Resource
    DataService dataService;
    @Resource
    GroupPlanService groupPlanService;
    @Resource
    ConfigDto configDto;

    @Scheduled(cron = "0 0 1 * * ?")
    private void syncJob() {
        log.info("每天1点定时任务执行开始");
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            LocalDate before = now.minusDays(1);
            DateParam dateParam = new DateParam();
            dateParam.setStartDate(formatter.format(before));
            dateParam.setEndDate(formatter.format(before));
            String token = this.loginPost();
            log.info("登录成功！token:" + "【" + token + "】");
            Integer size1 = this.stationInfoPost(token,dateParam);
            log.info("推送【StationInfo】数量:" + size1 + "【" + formatter.format(before) + "】");
            Integer size2 = this.groupPost(token,dateParam);
            log.info("推送【GroupInfo】数量:"  + size2 + "【" + formatter.format(before) + "】");
            dateParam.setStartDate(formatter.format(now));
            dateParam.setEndDate(formatter.format(now));
            Integer size3 = this.stationLightingPlanPost(token,dateParam);
            log.info("推送【StationLightingPlan】数量:"  + size3 + "【" + formatter.format(now) + "】");
        } catch (Exception e) {
            log.error("【每天1点定时任务执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("每天1点定时任务执行结束");
        }
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    private void syncJob2() {
        log.info("定时任务每5分钟执行开始");
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            LocalDate before = now.minusDays(1);
            DateParam dateParam = new DateParam();
            dateParam.setStartDate(formatter.format(before));
            dateParam.setEndDate(formatter.format(now));
            String token = this.loginPost();
            log.info("登录成功！token:" + "【" + token + "】");
            Integer size = this.stationStatusPost(token,dateParam);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            log.info("推送【StationStatus】数量:"  + size + "【" + sdf.format(new Date()) + "】");
        } catch (Exception e) {
            log.error("【定时任务每5分钟执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("定时任务每5分钟执行结束");
        }
    }

    private String loginPost() throws Exception {
        LoginPostParam loginPostParam = new LoginPostParam();
        loginPostParam.setPassword(configDto.getPassword());
        loginPostParam.setUsername(configDto.getUsername());
        loginPostParam.setVerifyCode(configDto.getVerifyCode());
        loginPostParam.setVerifyCodekey(configDto.getVerifyCodekey());
        LoginPost loginPost = new LoginPost();
        loginPost.setFunName(configDto.getFunName());
        loginPost.setServiceName(configDto.getServiceName());
        loginPost.setParam(loginPostParam);
        LoginPostResult result;
        try {
            String url = configDto.getPostHost() + "/api/service";
            HttpUtil httpUtil = new HttpUtil(url, configDto.getTimeout());
            result = httpUtil.loginPostUrl(loginPost);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return result.getData().getToken();
    }

    private Integer stationInfoPost(String token, DateParam param) throws Exception {
        Integer size = 0;
        List<DataStationInfoParam> sysList = stationInfoService.getSysList(param);
        for (DataStationInfoParam dataStationInfoParam : sysList) {
            param.setId(dataStationInfoParam.getSysId());
            List<DataStationInfo> dataList = stationInfoService.getStationList(param);
            dataStationInfoParam.setStationInfoList(dataList);
            size = size + dataList.size();
            try {
                String url = configDto.getPostHost() + "/lighting-rest/api/v1/data/StationInfo";
                HttpUtil httpUtil = new HttpUtil(url, configDto.getTimeout());
                httpUtil.postUrl2(dataStationInfoParam, token);
            } catch (Exception e) {
                throw new Exception(e.getMessage());
            }
        }
        return size;
    }

    private Integer groupPost(String token, DateParam param) throws Exception {
        Integer size = 0;
        List<DataGroupInfoParam> sysList = groupService.getSysList(param);
        for (DataGroupInfoParam dataGroupInfoParam : sysList) {
            param.setId(dataGroupInfoParam.getSysId());
            List<DataGroupInfo> dataList = groupService.getGroupList(param);
            for (DataGroupInfo dataGroupInfo : dataList) {
                param.setGroupId(dataGroupInfo.getGroupId());
                List<DataStationInfo> idList = stationInfoService.getIdList(param);
                dataGroupInfo.setStationNum(idList.size());
                if (idList.size() > 0) {
                    dataGroupInfo.setStationIdList(idList.stream().map(DataStationInfo::getStationId).toArray(Integer[]::new));
                }
            }
            dataGroupInfoParam.setGroupInfoList(dataList);
            size = size + dataList.size();
            try {
                String url = configDto.getPostHost() + "/lighting-rest/api/v1/data/GroupInfo";
                HttpUtil httpUtil = new HttpUtil(url, configDto.getTimeout());
                httpUtil.postUrl2(dataGroupInfoParam, token);
            } catch (Exception e) {
                throw new Exception(e.getMessage());
            }
        }
        return size;
    }

    private Integer stationStatusPost(String token, DateParam param) throws Exception {
        Integer size = 0;
        List<DataStationStatusParam> sysList = dataService.getSysList(param);
        for (DataStationStatusParam dataStationStatusParam : sysList) {
            param.setId(dataStationStatusParam.getSysId());
            List<DataStationStatus> dataList = dataService.getDataList(param);
            dataStationStatusParam.setStationStatusList(dataList);
            size = size + dataList.size();
            try {
                String url = configDto.getPostHost() + "/lighting-rest/api/v1/data/StationStatus";
                HttpUtil httpUtil = new HttpUtil(url, configDto.getTimeout());
                httpUtil.postUrl2(dataStationStatusParam, token);
            } catch (Exception e) {
                throw new Exception(e.getMessage());
            }
        }
        return size;
    }

    public Integer stationLightingPlanPost(String token, DateParam param) throws Exception {
        Integer size = 0;
        List<DataStationLightingPlanParam> sysList = groupPlanService.getSysList(param);
        for (DataStationLightingPlanParam dataStationLightingPlanParam : sysList) {
            param.setId(dataStationLightingPlanParam.getSysId());
            List<DataStationLightingPlanStatus> dataList = groupPlanService.getPlanList(param);
            dataStationLightingPlanParam.setLightingPlanList(dataList);
            size = size + dataList.size();
            try {
                String url = configDto.getPostHost() + "/lighting-rest/api/v1/data/StationLightingPlan";
                HttpUtil httpUtil = new HttpUtil(url, configDto.getTimeout());
                httpUtil.postUrl2(dataStationLightingPlanParam, token);
            } catch (Exception e) {
                throw new Exception(e.getMessage());
            }
        }
        return size;
    }
}
